<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');
$party                 = array();
$billDetailDebitArray  = array();
$billDetailCreditArray = array();
$total                 = array();
$opening               = array();
$openingFinal          = array();
$bullionmasterid       = isset($_POST['party']) ? $_POST['party'] : 0;
$vepaariBookDate       = 0;
$totNetWeight          = 0;
$billBookDetailId      = 0;
$billBookNo            = 0;
$showPrev              = 0;
$fromDateSelected      = 0;
$toDate                = isset($_REQUEST['toDate']) ? $_REQUEST['toDate'] : "";
$drCounter             = 0;
$crCounter             = 0;
$netfine               = 0;
$netamount             = 0;
$openingFinalFineDr    = 0;
$openingFinalFineCr    = 0;
$openingFinalAmountDr  = 0;
$openingFinalAmountCr  = 0;
$total['DrFine']       = 0;
$total['DrQty']        = 0;
$total['DrLabour']     = 0;
$total['DrAmount']     = 0;
$total['CrFine']       = 0;
$total['CrQty']        = 0;
$total['CrLabour']     = 0;
$total['CrAmount']     = 0;
$totalFineCr           = 0;
$totalFineDr           = 0;
$totalAmountCr         = 0;
$totalAmountDr         = 0;
$partyName             = "";
$fromDate              = "";
if(!isset($_REQUEST['party']) && !isset($_REQUEST['partyId']))
  $partyId = 0;
else
  $partyId = isset($_REQUEST['party']) ? $_REQUEST['party'] : $_GET['partyId'] ;

if(isset($_REQUEST['fromDateYear']))
  $fromDateSelected = isset($_REQUEST['fromDateYear']) ? $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'] : 0;
else
  $fromDateSelected  = date("Y-m-d");
if(isset($_REQUEST['toDateYear']))
  $toDate  = isset($_REQUEST['toDateYear']) ? $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'] : 0;
else
  $toDate  = date("Y-m-d");
 
$selectParty = "SELECT partyId,partyName,partyFromDate 
                  FROM party
                 ORDER BY partyName DESC";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
$party['partyId'][$a]   = 0;
$party['partyName'][$a] = "Select";
$a++;
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
  $party['partyId'][$a]   = $partyRow['partyId'];
  $party['partyName'][$a] = $partyRow['partyName'];
  $party['partyFromDate'][$a] = $partyRow['partyFromDate'];
  $a++;

  if(isset($_REQUEST['bdate']) == "1" && $partyRow['partyId'] == $_REQUEST['party'])
  {
    $fromDate = $partyRow['partyFromDate'];

    if(!isset($_REQUEST['submitBtn']))
      $fromDateSelected  = $partyRow['partyFromDate'];    
  }
}

if(isset($_REQUEST['party']) && $_REQUEST['party'] > 0)
{
  $selectbillDetailArray = "SELECT vepaaribilldetail.vepaariBookMasterId,vepaaribilldetail.vepaariBookDetailId,
                                   vepaaribillmaster.vepaariBookMasterId,vepaaribillmaster.partyId,
                                   DATE_FORMAT(vepaaribillmaster.vepaariBookDate,'%Y-%m-%d') AS vepaariBookDate,
                                   vepaaribilldetail.particulars,party.partyName, vepaaribilldetail.netWeight,vepaaribilldetail.touch,
                                   vepaaribilldetail.waste,vepaaribilldetail.fine,vepaaribilldetail.qty,vepaaribilldetail.labour,
                                   vepaaribilldetail.amount,vepaaribilldetail.CrDr,vepaaribilldetail.rojmelId,
                                   vepaaribillmaster.billNo
                              FROM vepaaribilldetail
                              JOIN vepaaribillmaster ON vepaaribilldetail.vepaariBookMasterId = vepaaribillmaster.vepaariBookMasterId
                              JOIN party ON party.partyId = vepaaribillmaster.partyId
                             WHERE vepaaribillmaster.partyId = ".$_REQUEST['party']."
                               AND vepaaribillmaster.vepaariBookDate <= '".$toDate."'
                          ORDER BY vepaaribilldetail.vepaariBookMasterId";
  $selectbillDetailArrayRes = mysql_query($selectbillDetailArray);
  //$vepaaribillDetailRow = mysql_num_rows($selectbillDetailArrayRes);
  
  $opening['drNetWeight'] = 0; 
  $opening['drTouch']     = 0; 
  $opening['drWaste']     = 0; 
  $opening['drTouch']     = 0; 
  $opening['drFine']      = 0; 
  $opening['drQty']       = 0; 
  $opening['drLabour']    = 0; 
  $opening['drAmount']    = 0; 
  $opening['crNetWeight'] = 0; 
  $opening['crLabour']    = 0; 
  $opening['crWaste']     = 0; 
  $opening['crTouch']     = 0; 
  $opening['crFine']      = 0; 
  $opening['crQty']       = 0; 
  $opening['crLabour']    = 0; 
  $opening['crAmount']    = 0; 
  
  while($vepaaribillDetailRow = mysql_fetch_array($selectbillDetailArrayRes))
  {  
  	if($vepaaribillDetailRow['vepaariBookDate'] >= $fromDateSelected)
    { 
  	  if($vepaaribillDetailRow['CrDr'] == 'Dr')
      {
        $billDetailDebitArray[$drCounter]['vepaariBookDetailId']  = $vepaaribillDetailRow['vepaariBookDetailId'];
        $billDetailDebitArray[$drCounter]['vepaariBookMasterId']  = $vepaaribillDetailRow['vepaariBookMasterId'];
        $billDetailDebitArray[$drCounter]['vepaariBookDate']      = $vepaaribillDetailRow['vepaariBookDate'];
        $billDetailDebitArray[$drCounter]['particulars']          = $vepaaribillDetailRow['particulars'];
        $billDetailDebitArray[$drCounter]['netWeight']            = $vepaaribillDetailRow['netWeight'];
        $billDetailDebitArray[$drCounter]['partyId']              = $vepaaribillDetailRow['partyId'];
        $billDetailDebitArray[$drCounter]['partyName']            = $vepaaribillDetailRow['partyName'];
        $billDetailDebitArray[$drCounter]['touch']                = $vepaaribillDetailRow['touch'];
        $billDetailDebitArray[$drCounter]['waste']                = $vepaaribillDetailRow['waste'];
        $billDetailDebitArray[$drCounter]['fine']                 = $vepaaribillDetailRow['fine'];
        $billDetailDebitArray[$drCounter]['qty']                  = $vepaaribillDetailRow['qty'];
        $billDetailDebitArray[$drCounter]['labour']               = $vepaaribillDetailRow['labour'];
        $billDetailDebitArray[$drCounter]['amount']               = $vepaaribillDetailRow['amount'];
        $drCounter++;
      }
      else
      {
        $billDetailCreditArray[$crCounter]['vepaariBookDetailId'] = $vepaaribillDetailRow['vepaariBookDetailId'];
        $billDetailCreditArray[$crCounter]['vepaariBookMasterId'] = $vepaaribillDetailRow['vepaariBookMasterId'];
        $billDetailCreditArray[$crCounter]['vepaariBookDate']     = $vepaaribillDetailRow['vepaariBookDate'];
        $billDetailCreditArray[$crCounter]['particulars']         = $vepaaribillDetailRow['particulars'];
        $billDetailCreditArray[$crCounter]['netWeight']           = $vepaaribillDetailRow['netWeight'];
        $billDetailCreditArray[$crCounter]['partyId']             = $vepaaribillDetailRow['partyId'];
        $billDetailCreditArray[$crCounter]['partyName']           = $vepaaribillDetailRow['partyName'];
        $billDetailCreditArray[$crCounter]['touch']               = $vepaaribillDetailRow['touch'];
        $billDetailCreditArray[$crCounter]['waste']               = $vepaaribillDetailRow['waste'];
        $billDetailCreditArray[$crCounter]['fine']                = $vepaaribillDetailRow['fine'];
        $billDetailCreditArray[$crCounter]['qty']                 = $vepaaribillDetailRow['qty'];
        $billDetailCreditArray[$crCounter]['labour']              = $vepaaribillDetailRow['labour'];
        $billDetailCreditArray[$crCounter]['amount']              = $vepaaribillDetailRow['amount'];
        $crCounter++;
      }
      if($vepaaribillDetailRow['CrDr'] == 'Dr')
  	  {
  	    $total['DrFine']      += $vepaaribillDetailRow['fine'];
  	    $total['DrQty']       += $vepaaribillDetailRow['qty'];
  	    $total['DrLabour']    += $vepaaribillDetailRow['labour'];
  	    $total['DrAmount']    += $vepaaribillDetailRow['amount'];
  	  }
  	  else
  	  {
  	  $total['CrFine']      += $vepaaribillDetailRow['fine'];
  	  $total['CrQty']       += $vepaaribillDetailRow['qty'];         
  	  $total['CrLabour']    += $vepaaribillDetailRow['labour'];      
  	  $total['CrAmount']    += $vepaaribillDetailRow['amount'];       	
  	  }
    }
    else
    {      
    	if($vepaaribillDetailRow['CrDr'] == 'Dr')
      {
    	  $opening['drNetWeight'] += $vepaaribillDetailRow['netWeight'];
        $opening['drTouch']     += $vepaaribillDetailRow['touch'];
        $opening['drWaste']     += $vepaaribillDetailRow['waste'];
        $opening['drFine']      += $vepaaribillDetailRow['fine'];
        $opening['drQty']       += $vepaaribillDetailRow['qty'];
        $opening['drLabour']    += $vepaaribillDetailRow['labour'];
        $opening['drAmount']    += $vepaaribillDetailRow['amount'];   
      }
    	else
    	{
    		$opening['crNetWeight'] += $vepaaribillDetailRow['netWeight'];
        $opening['crTouch']     += $vepaaribillDetailRow['touch'];
        $opening['crWaste']     += $vepaaribillDetailRow['waste'];
        $opening['crFine']      += $vepaaribillDetailRow['fine'];
        $opening['crQty']       += $vepaaribillDetailRow['qty'];
        $opening['crLabour']    += $vepaaribillDetailRow['labour'];
        $opening['crAmount']    += $vepaaribillDetailRow['amount'];
      }
      $openingFinal['netFineOpening'] = $opening['drFine'] - $opening['crFine'];
      $openingFinal['netAmountOpening'] = $opening['drAmount'] - $opening['crAmount'];
      if($openingFinal['netFineOpening'] > 0 )
      {
        $openingFinalFineDr = $openingFinal['netFineOpening']; 
        $openingFinalFineCr = "";
      }
      else 
      {
        $openingFinalFineCr = $openingFinal['netFineOpening']; 
        $openingFinalFineDr = "";
      }
       if($openingFinal['netAmountOpening'] > 0 )
      {
        $openingFinalAmountCr = ""; 
        $openingFinalAmountDr = $openingFinal['netAmountOpening'];
      }
      else 
      {
        $openingFinalAmountCr = $openingFinal['netAmountOpening']; 
        $openingFinalAmountDr = "";
      }
    }
  }
 $selectbillDetailArray = "SELECT vepaariBookMasterId,partyId,DATE_FORMAT(vepaariBookDate,'%Y-%m-%d') AS vepaariBookDate,billNo,totalFine,totalAmount
                              FROM vepaaribillmaster 
                             WHERE partyId = ".$_REQUEST['party']."
                               AND vepaariBookDate <= '".$toDate."'
                               AND billNo >= 0";
  $selectbillDetailArrayRes = mysql_query($selectbillDetailArray);
  $vepaaribillDetailRow = mysql_num_rows($selectbillDetailArrayRes);
  $billWiseFineTot   = 0;
  $billWiseAmountTot = 0;
  while($vepaaribillDetailRow = mysql_fetch_array($selectbillDetailArrayRes))
  { 
    $vepaaribillDetailRow['vepaariBookDate']. " >= ".$fromDateSelected;
  	if($vepaaribillDetailRow['vepaariBookDate'] >= $fromDateSelected)
    {
      $billDetailDebitArray[$drCounter]['billNo']               = $vepaaribillDetailRow['billNo'];
      $billDetailDebitArray[$drCounter]['vepaariBookMasterId']  = $vepaaribillDetailRow['vepaariBookMasterId'];
      $billDetailDebitArray[$drCounter]['vepaariBookDate']      = $vepaaribillDetailRow['vepaariBookDate'];
      $billDetailDebitArray[$drCounter]['fine']                 = $vepaaribillDetailRow['totalFine'];
      $billDetailDebitArray[$drCounter]['amount']               = $vepaaribillDetailRow['totalAmount'];
      $billWiseFineTot   += $vepaaribillDetailRow['totalFine'];
      $billWiseAmountTot += $vepaaribillDetailRow['totalAmount'];
      $drCounter++;
    }
    else
    {
      $openingFinalAmountDr += $vepaaribillDetailRow['totalAmount'];
      $openingFinalFineDr   += $vepaaribillDetailRow['totalFine'];
    }
  }
  if($openingFinalFineCr < 0)
    $totalFineCr  = $openingFinalFineCr - $total['CrFine'];
  else
    $totalFineCr  = $total['CrFine'] + $openingFinalFineCr;
    
  
  if($openingFinalAmountCr < 0)
    $totalAmountCr = $openingFinalAmountCr - $total['CrAmount'];
  else
    $totalAmountCr = $total['CrAmount'] + $openingFinalAmountCr;
     

  $totalFineDr  = $total['DrFine'] + $billWiseFineTot + $openingFinalFineDr;   
  $totalAmountDr = $total['DrAmount'] + $billWiseAmountTot + $openingFinalAmountDr;
  
  if($totalFineDr > $totalFineCr)
  {
    if($totalFineDr==0)
      $total['totFine'] = ($totalFineCr - $totalFineDr);
    elseif($totalFineCr==0)
      $total['totFine'] = ($totalFineDr - $totalFineCr);
    elseif($totalFineCr < 0)
      $total['totFine'] = ($totalFineDr + $totalFineCr);
    elseif($totalFineDr < 0)
      $total['totFine'] = ($totalFineDr + $totalFineCr)."<br>";
    else 
      $total['totFine']   = ($totalFineDr - $totalFineCr)."<br>";
  }
  elseif($totalFineDr < $totalFineCr)
  {
    if($totalFineDr==0)
      $total['totFine'] = ($totalFineDr - $totalFineCr);
    elseif($totalFineCr==0)
      $total['totFine'] = ($totalFineCr - $totalFineDr);
    elseif($totalFineCr < 0)
      $total['totFine'] = ($totalFineDr + $totalFineCr);
    elseif($totalFineDr < 0)
      $total['totFine'] = ($totalFineDr + $totalFineCr);
    else 
     $total['totFine']   = ($totalFineDr - $totalFineCr);
  }
    
  if($totalAmountDr > $totalAmountCr)
  {
    if($totalAmountDr==0)
      $total['totAmount'] = ($totalAmountCr - $totalAmountDr);
    elseif($totalAmountCr==0)
      $total['totAmount'] = ($totalAmountDr - $totalAmountCr);
    elseif($totalAmountCr < 0)
      $total['totAmount'] = ($totalAmountDr + $totalAmountCr);
    elseif($totalAmountDr < 0)
      $total['totAmount'] = ($totalAmountDr + $totalAmountCr);
    else 
      $total['totAmount'] = ($totalAmountDr - $totalAmountCr);
  }
  elseif($totalAmountDr < $totalAmountCr)
  {
    if($totalAmountDr==0)
      $total['totAmount'] = ($totalAmountDr - $totalAmountCr);
    elseif($totalAmountCr==0)
      $total['totAmount'] = ($totalAmountCr - $totalAmountDr);
    elseif($totalAmountCr < 0)
      $total['totAmount'] = ($totalAmountDr + $totalAmountCr);
    elseif($totalAmountDr < 0)
      $total['totAmount'] = ($totalAmountDr + $totalAmountCr);
    else 
      $total['totAmount'] = ($totalAmountDr - $totalAmountCr);
  }
}

$billDetailCreditArrayCount = count($billDetailCreditArray);
$billDetailDebitArrayCount  = count($billDetailDebitArray);
if($billDetailCreditArrayCount < $billDetailDebitArrayCount)
{
	$extraBillDetailCredit = $billDetailDebitArrayCount - $billDetailCreditArrayCount;
	$extraBillDetailDebit  = 0;
}
else
{
	$extraBillDetailCredit = 0;
	$extraBillDetailDebit  = $billDetailCreditArrayCount - $billDetailDebitArrayCount; // if both are same, than this will be 0
}

$vepaariBookDateArr = array();

foreach ($billDetailDebitArray as $key => $row) {
    $vepaariBookDateArr[$key]  = $row['vepaariBookDate'];
}

array_multisort($vepaariBookDateArr,SORT_ASC,$billDetailDebitArray);
//var_dump($billDetailDebitArray);

$smarty = new Smarty;
require("bottom.php");
$smarty->assign("netfine",$netfine);
$smarty->assign("netamount",$netamount);
$smarty->assign("bullionmasterid",$bullionmasterid);
$smarty->assign("billDetailDebitArray",$billDetailDebitArray);
$smarty->assign("fromDateSelected",$fromDateSelected);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("billDetailCreditArray",$billDetailCreditArray);
$smarty->assign("total",$total);
$smarty->assign("party",$party);
$smarty->assign("partyId",$partyId);
$smarty->assign("partyName",$partyName);
$smarty->assign("toDate",$toDate);
$smarty->assign("opening",$opening);
$smarty->assign("openingFinal",$openingFinal);
$smarty->assign("openingFinalFineDr",$openingFinalFineDr);
$smarty->assign("openingFinalFineCr",$openingFinalFineCr);
$smarty->assign("openingFinalAmountDr",$openingFinalAmountDr);
$smarty->assign("openingFinalAmountCr",$openingFinalAmountCr);
$smarty->assign("extraBillDetailCredit",$extraBillDetailCredit);
$smarty->assign("extraBillDetailDebit",$extraBillDetailDebit);
$smarty->assign("totalFineCr",$totalFineCr);
$smarty->assign("totalFineDr",$totalFineDr);
$smarty->assign("totalAmountCr",$totalAmountCr);
$smarty->assign("totalAmountDr",$totalAmountDr);
$smarty->display("vepaariBookListNew.tpl");
?>